using System;
using System.Diagnostics;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using SukiUI;
using SukiUI.Enums;
using SukiUI.Models;

namespace NewUI.Views;

public partial class Main : UserControl
{
    private bool isLight = true;
    private static string LigthImage = "avares://NewUI/Assets/Images/white-main.png";
    private static string DarkImage = "avares://NewUI/Assets/Images/dark-main.jpg";
    
    public Main()
    {
        InitializeComponent();
    }

    private void SwitchTheme(object? sender, RoutedEventArgs e)
    {
        if (isLight)
        {
            SukiTheme.GetInstance().ChangeColorTheme(SukiColor.Orange);
            ChangeBackgroundImage(DarkImage);
        }
        else
        {
            // var PurpleTheme = new SukiColorTheme("Purple", Colors.Purple, Colors.DarkBlue);
            SukiTheme.GetInstance().ChangeColorTheme(SukiColor.Blue);
            ChangeBackgroundImage(LigthImage);
        }
        SukiTheme.GetInstance().SwitchBaseTheme();
        isLight = !isLight;
        
    }

    private void ChangeBackgroundImage(string imagePath)
    {
        try
        {
            // 使用 Avalonia 的资源加载器打开流
            using (var stream = AssetLoader.Open(new Uri(imagePath)))
            {
                // 创建位图
                var bitmap = new Bitmap(stream);
                // 创建图片画刷
                var imageBrush = new ImageBrush
                {
                    Source = bitmap,
                    Stretch = Stretch.Fill,  // 推荐使用 UniformToFill 保持比例
                    // AlignmentX = AlignmentX.Center,   // 水平居中
                    // AlignmentY = AlignmentY.Center,   // 垂直居中
                    Opacity = 0.4                    // 确保完全可见
                };
                // 设置 Border 的背景
                if (MainBorder != null)
                {
                    MainBorder.Background = imageBrush;
                }
            }
        }
        catch (Exception ex)
        {
            // 处理异常（文件不存在/路径错误等）
            Debug.WriteLine($"加载背景图片失败: {ex.Message}");
        }
    }
    
}